#include "dialog.h"
#include "noisemaker.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QTextBrowser>
#include <QString>
#include <QProgressBar>
#include <QIntValidator>


Dialog::Dialog()
{

    setWindowTitle(tr("Dialog"));

// ********setup layouts********
    QVBoxLayout* mainLayoutPtr = new QVBoxLayout(this);
    QHBoxLayout* TitleLayoutPtr = new QHBoxLayout;
    QGridLayout* LinesLayoutPtr = new QGridLayout;
    QHBoxLayout* AverageLayoutPtr = new QHBoxLayout;
    QHBoxLayout* InstrucitonLayoutPtr = new QHBoxLayout;
    QHBoxLayout* buttonLayoutPtr = new QHBoxLayout;

// ********set objects in layouts*******
    mainLayoutPtr->addLayout(TitleLayoutPtr);
    TitleLayoutPtr->addStretch();

    mainLayoutPtr->addLayout(LinesLayoutPtr);
    mainLayoutPtr->addStretch();

    mainLayoutPtr->addLayout(AverageLayoutPtr);
    AverageLayoutPtr->addStretch();
    mainLayoutPtr->addStretch();

    mainLayoutPtr->addLayout(InstrucitonLayoutPtr);

    mainLayoutPtr->addStretch();
    mainLayoutPtr->addLayout(buttonLayoutPtr);

// ********Label inserting*******

    TitleLabelPtr = new QLabel(tr("Food Critic"));
    TitleLayoutPtr->addWidget(TitleLabelPtr);
    TitleLayoutPtr->addStretch();
    QFont font("Helvetica", 25, QFont::Bold);
    TitleLabelPtr->setFont(font);

    TasteLabelPtr = new QLabel(tr("Taste"));
    LinesLayoutPtr->addWidget(TasteLabelPtr, 0, 0);


    PresentationLabelPtr = new QLabel(tr("Presentation"));
    LinesLayoutPtr->addWidget(PresentationLabelPtr, 1, 0);


    AtmosphereLabelPtr = new QLabel(tr("Atmosphere"));
    LinesLayoutPtr->addWidget(AtmosphereLabelPtr, 2, 0);


    ServiceLabelPtr = new QLabel(tr("Service"));
    LinesLayoutPtr->addWidget(ServiceLabelPtr, 3, 0);


    CostLabelPtr = new QLabel(tr("Cost"));
    LinesLayoutPtr->addWidget(CostLabelPtr, 4, 0);


    AverageLabelPtr = new QLabel(tr("Overall Dining Experience"));
    AverageLayoutPtr->addWidget(AverageLabelPtr);


    QTextBrowser* tb = new QTextBrowser(this);
    QString s = "Directions:\n"
                "\n"
                "Enter an integer from 0 (Bad) to 5 (Good) for the five categories (Taste, Presentation, Atmosphere, Service, and Cost)"
                "and the Overall Dining Experience will be computed as the average of the five inputs."
                "An audible beep indicates the you have attempted to input a non-integer or an integer outside of the allowable range."
                "Progress bars will provide feedback.\n\n"

                "Use Clear to start over or Quit to terminate the application.";

    tb->setText(s);
    InstrucitonLayoutPtr->addWidget(tb);



    clearButtonPtr = new QPushButton(tr("Clear"));  // Allocate and initialize clear button
                                                    // widget
    quitButtonPtr = new QPushButton(tr("Quit"));    // Allocate and initialize quit button widget
    quitButtonPtr->setDefault(true);                // Make it default button of dialog
    buttonLayoutPtr->addWidget(clearButtonPtr);     // Add clear button to button layout
    buttonLayoutPtr->addStretch();                  // Add stretch
    buttonLayoutPtr->addWidget(quitButtonPtr);      // Add quit button to button layout

//********Line edits**********

    TasteLineEditPtr = new QLineEdit;
    TasteLineEditPtr->setMaxLength(MAXIMUM_LENGTH);
    TasteLineEditPtr->setFrame(true);
    LinesLayoutPtr->addWidget(TasteLineEditPtr, 0, 1);

    PresentationLineEditPtr = new QLineEdit;
    PresentationLineEditPtr->setMaxLength(MAXIMUM_LENGTH);
    PresentationLineEditPtr->setFrame(true);
    LinesLayoutPtr->addWidget(PresentationLineEditPtr, 1, 1);

    AtmosphereLineEditPtr = new QLineEdit;
    AtmosphereLineEditPtr->setMaxLength(MAXIMUM_LENGTH);
    AtmosphereLineEditPtr->setFrame(true);
    LinesLayoutPtr->addWidget(AtmosphereLineEditPtr, 2, 1);

    ServiceLineEditPtr = new QLineEdit;
    ServiceLineEditPtr->setMaxLength(MAXIMUM_LENGTH);
    ServiceLineEditPtr->setFrame(true);
    LinesLayoutPtr->addWidget(ServiceLineEditPtr, 3, 1);

    CostLineEditPtr = new QLineEdit;
    CostLineEditPtr->setMaxLength(MAXIMUM_LENGTH);
    CostLineEditPtr->setFrame(true);
    LinesLayoutPtr->addWidget(CostLineEditPtr, 4, 1);

    AverageLineEditPtr = new QLineEdit;
    AverageLineEditPtr->setFrame(true);
    AverageLineEditPtr->setText("0");
    AverageLineEditPtr->setReadOnly(true);
    AverageLayoutPtr->addWidget(AverageLineEditPtr);
    AverageLayoutPtr->addStretch();

// ********Validator space*******
    QIntValidator* intVal = new QIntValidator( 0, 5, TasteLineEditPtr );
    TasteLineEditPtr->setValidator(intVal);

    noiseMaker* loud = new noiseMaker(0, 5, TasteLineEditPtr);
    TasteLineEditPtr->setValidator(loud);

    QIntValidator* intVal1 = new QIntValidator( 0, 5, PresentationLineEditPtr );
    PresentationLineEditPtr->setValidator(intVal1);

    noiseMaker* loud1 = new noiseMaker(0, 5, PresentationLineEditPtr);
    PresentationLineEditPtr->setValidator(loud1);

    QIntValidator* intVal2 = new QIntValidator( 0, 5, AtmosphereLineEditPtr );
    AtmosphereLineEditPtr->setValidator(intVal2);

    noiseMaker* loud2 = new noiseMaker(0, 5, AtmosphereLineEditPtr);
    AtmosphereLineEditPtr->setValidator(loud2);

    QIntValidator* intVal3 = new QIntValidator( 0, 5, ServiceLineEditPtr );
    ServiceLineEditPtr->setValidator(intVal3);

    noiseMaker* loud3 = new noiseMaker(0, 5, ServiceLineEditPtr);
    ServiceLineEditPtr->setValidator(loud3);

    QIntValidator* intVal4 = new QIntValidator( 0, 5, CostLineEditPtr );
    CostLineEditPtr->setValidator(intVal4);

    noiseMaker* loud4 = new noiseMaker(0, 5, CostLineEditPtr);
    CostLineEditPtr->setValidator(loud4);

// ********Progress Bars*******
    TasteProgPtr = new QProgressBar;
    LinesLayoutPtr->addWidget(TasteProgPtr, 0, 2);

    PresentationProgPtr = new QProgressBar;
    LinesLayoutPtr->addWidget(PresentationProgPtr, 1, 2);

    AtmosphereProgPtr = new QProgressBar;
    LinesLayoutPtr->addWidget(AtmosphereProgPtr, 2, 2);

    ServiceProgPtr = new QProgressBar;
    LinesLayoutPtr->addWidget(ServiceProgPtr, 3, 2);

    CostProgPtr = new QProgressBar;
    LinesLayoutPtr->addWidget(CostProgPtr, 4, 2);

// ********Prog null*******
    TasteProgPtr->setValue(0);
    PresentationProgPtr->setValue(0);
    AtmosphereProgPtr->setValue(0);
    ServiceProgPtr->setValue(0);
    CostProgPtr->setValue(0);


// ********Buttons/slots functioning*******

    connect(quitButtonPtr, SIGNAL(clicked()), this, SLOT(accept()));     // Connect quit button
    connect(clearButtonPtr, SIGNAL(clicked()), this, SLOT(clearText())); // Connect clear button
    connect(TasteLineEditPtr, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(const QString&)));
    connect(PresentationLineEditPtr, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(const QString&)));
    connect(AtmosphereLineEditPtr, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(const QString&)));
    connect(ServiceLineEditPtr, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(const QString&)));
    connect(CostLineEditPtr, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(const QString&)));

}

void Dialog::textChanged(const QString& value)
{
    int prog1 = TasteLineEditPtr->text().toInt();
    int prog2 = PresentationLineEditPtr->text().toInt();
    int prog3 = AtmosphereLineEditPtr->text().toInt();
    int prog4 = ServiceLineEditPtr->text().toInt();
    int prog5 = CostLineEditPtr->text().toInt();

    TasteProgPtr->setValue(prog1*20);

    PresentationProgPtr->setValue(prog2*20);

    AtmosphereProgPtr->setValue(prog3*20);

    ServiceProgPtr->setValue(prog4*20);

    CostProgPtr->setValue(prog5*20);

    double sum = prog1 + prog2 + prog3 + prog4 + prog5;

    double avg = sum/5;

    QString str;

    str.append(QString("%1").arg(avg));

    AverageLineEditPtr->setText(str);


} // End textChanged()


void Dialog::clearText()
{
    TasteLineEditPtr->setText("");
    PresentationLineEditPtr->setText("");
    AtmosphereLineEditPtr->setText("");
    ServiceLineEditPtr->setText("");
    CostLineEditPtr->setText("");

} // End of clear
